﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Admin.业务界面
{
    public partial class 自动分配 : Form
    {
        List<Models.TeaChoice> ts;
        List<Models.StuChoice> gs;
        List<Models.Pair> pairs;
        List<Models.Pair> sorted_pair;

        int t_choice;
        int g_choice;

        public 自动分配()
        {
            InitializeComponent();
        }

        private void 自动分配_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Hide();
            //管理员主界面 f1 = new 管理员主界面();
            //f1.ShowDialog();
        }

        private void 自动分配_Load(object sender, EventArgs e)//自动分配加载页面
        {
            if (LogInfo.Sys_Config.tearch_choice == null || LogInfo.Sys_Config.group_choice == null)
                return;
            t_choice = (int)LogInfo.Sys_Config.tearch_choice;
            g_choice = (int)LogInfo.Sys_Config.group_choice;


            BLL.B_Admin b_T = new BLL.B_Admin();
            BLL.B_Admin b_G = new BLL.B_Admin();

            ts = b_T.GetList_T_C();
            gs = b_G.GetList_S_C();

            dataGridView1.DataSource = new BindingList<StuChoice>(gs);
            dataGridView3.DataSource = new BindingList<TeaChoice>(ts);
            count_score();

            int start_idx = 0;
            for (int i = 5; i - start_idx > g_choice; i--)
            {
                dataGridView1.Columns[i].Visible = false;
            }

            for (int i = 5; i - start_idx > t_choice; i--)
            {
                dataGridView3.Columns[i].Visible = false;
                dataGridView3.Columns[i + 5].Visible = false;
            }

            dataGridView2.Columns[0].Visible = false;

            start_idx = 2;
            for (int i = 7; i - start_idx > g_choice; i--)
            {
                dataGridView2.Columns[i].Visible = false;
            }

            start_idx = 7;
            for (int i = 10; i - start_idx > t_choice; i--)
            {
                dataGridView2.Columns[i].Visible = false;
            }
        }

        void count_score()//计算权值
        {
            pairs = new List<Pair>();

            foreach (var g in gs)
            {
                foreach (var t in ts)
                {
                    int[] S_c = { 0, 0, 0 };
                    int[] T_c = { 0, 0, 0, 0, 0 };

                    double marks_s = 0.0;
                    double marks_t = 0.0;
                    
                    //set 1
                    if (g.Choice_1 == t.Teacher_id) 
                        S_c[0] = 1;
                    else if (g.Choice_2 == t.Teacher_id) 
                        S_c[1] = 1;
                    else if (g.Choice_3 == t.Teacher_id)
                        S_c[2] = 1;                  

                    for (int n = 0; n < g_choice; n++)
                    {
                        if (S_c[n] == 1)
                        {
                            //
                            marks_s = Math.Pow(2, g_choice - n);
                            break;
                        }
                    }

                    //置1法
                    if (t.Choice_1 == g.Group_id) 
                        T_c[0] = 1;
                    else if (t.Choice_2 == g.Group_id)
                        T_c[1] = 1;
                    else if (t.Choice_3 == g.Group_id)
                        T_c[2] = 1;
                    else if (t.Choice_4 == g.Group_id) 
                        T_c[3] = 1;
                    else if (t.Choice_5 == g.Group_id) 
                        T_c[4] = 1;                  

                    for (int n = 0; n < t_choice; n++)
                    {
                        if (T_c[n] == 1)
                        {
                            // 计算方法
                            marks_t = Math.Pow(2, t_choice - n);
                            break;
                        }
                        
                    }

                    Models.Pair pair = new Models.Pair();

                    pair.group_id = g.Group_id;
                    pair.teacher_id = t.Teacher_id;
                    pair.stu_c1 = g.Choice_1;
                    pair.stu_c2 = g.Choice_2;
                    pair.stu_c3 = g.Choice_3; 
                    
                    pair.teacher_c1 = t.Choice_1;
                    pair.teacher_c2 = t.Choice_2;
                    pair.teacher_c3 = t.Choice_3;
                    pair.teacher_c4 = t.Choice_4;
                    pair.teacher_c5 = t.Choice_5;    
                    
                    //最终分值
                    pair.score = marks_s + marks_t * 1.1;
                    pair.assigned = 0;

                    pairs.Add(pair);
                }
            }

            dataGridView2.DataSource = new BindingList<Pair>(pairs);
        }



        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)//过滤按钮
        {
            sorted_pair = pairs.Where(p => p.score > 0).OrderByDescending(p => p.score).ToList();
            dataGridView2.DataSource = new BindingList<Pair>(sorted_pair);
        }

        private void button4_Click(object sender, EventArgs e)//自动分配按钮
        {
            //帮助函数
            clear_assigned();

            int assigned, t_rescount;

                foreach (var pair in sorted_pair)//自动分配前要先过滤数据
                {
                    assigned = (int)pair.assigned;
                    Models.TeaChoice teacher = ts.Where(t => t.Teacher_id == pair.teacher_id).FirstOrDefault();
                    Models.StuChoice group = gs.Where(g => g.Group_id == pair.group_id).FirstOrDefault();

                    t_rescount = teacher.res_count;

                    if (t_rescount == t_choice) 
                        continue;//如果这个老师的名额已满，则下一行

                    if (assigned == 1) 
                        continue; //如果这一行已经经过分配运算，则下一行


                    for (int i = 1; i <= LogInfo.Sys_Config.tearch_choice; i++)
                    {
                        if (get_tChoice(teacher, $"Assign_{i}") == -1)
                        {
                            //设定选择
                            set_tChoice(teacher, $"Assign_{i}", group.Group_id);
                            break;
                        }
                    }

                    teacher.res_count += 1;
                    group.T_id_first = teacher.Teacher_id;

                    pairs.Where(p => p.group_id == group.Group_id).ToList().ForEach(p => p.assigned = 1);
                }
                BLL.B_Admin b_G = new BLL.B_Admin();
                BLL.B_Admin b_T = new BLL.B_Admin();

            try
            {
                b_G.Save_result_S(gs);
                b_T.Save_result_T(ts);
                MessageBox.Show("分配成功");

                dataGridView1.DataSource = new BindingList<StuChoice>(gs);
                dataGridView3.DataSource = new BindingList<TeaChoice>(ts);
            }
            catch (Tools.Modify_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private int get_tChoice(Models.TeaChoice t_Data, string pro_name)
        {
            return (int)t_Data.GetType().GetProperty(pro_name).GetValue(t_Data, null); ;
        }

        private void set_tChoice(Models.TeaChoice t_Data, string pro_name, int g_id)
        {
            //
            t_Data.GetType().GetProperty(pro_name).SetValue(t_Data, g_id);
        }

        //置负函数
        private void clear_assigned()
        {
            ts.ForEach(t => { t.Assign_1 = -1; t.Assign_2 = -1; t.Assign_3 = -1; t.Assign_4 = -1; t.Assign_5 = -1; t.res_count = 0; });
            gs.ForEach(g => { g.T_id_first = -1; });
        }

        private void button5_Click(object sender, EventArgs e)//发布按钮
        {
            BLL.B_Admin b_Release_Result = new BLL.B_Admin();
            try
            {
                var res = b_Release_Result.Save_result_A(ts, gs);
                if (res) MessageBox.Show("发布成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)//ID对照按钮
        {
            ID对照 view_Data = new ID对照();
            view_Data.Show();
        }

        private void button2_Click(object sender, EventArgs e)//调整结果按钮
        {
            手动分配 manual_Adjust = new 手动分配();
            manual_Adjust.ShowDialog();
        }
    }
}
